import { getlistApi, addlistApi, updatelistApi, dellistApi } from "@/api/exam"

const state = {
    list: [],
    total: null,
    currentPage: 1,
    pageSize: 5,
    orderby: 'id',
    sort: 'desc',
    searchVal: ""
}

const mutations = {
    SET_PAGESIZE(state, val) {
        state.pageSize = val;
    },
    SET_CURRENTPAGE(state, val) {
        state.currentPage = val
    },
    SET_LIST(state, r) {
        state.total = r.total;
        r.data.map(v => {
            v.answers = JSON.parse(v.answers)
            v.options = JSON.parse(v.options || '[]')
        })
        state.list = r.data;
    },
    SET_SEARCHVAL(state, val) {
        state.searchVal = val;
    },
}

const actions = {
    async getlist({ commit, state }) {
        let r = await getlistApi({
            currentPage: state.currentPage - 1,
            pageSize: state.pageSize,
            orderby: state.orderby,
            sort: state.sort,
            searchVal: state.searchVal
        })
        commit("SET_LIST", r)
    },
    async addlist({ }, form) {
        await addlistApi(form)
    },
    async updatelist({ }, form) {
        await updatelistApi(form)
    },
    async dellist({ }, id) {
        await dellistApi({ id })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}